import { Controller, Get, Put, Param, Body, Delete, Post, Patch, Query } from '@nestjs/common';
import { ArticleService } from './article.service';
import { ArticleResponseDto, UpdateArticleDto } from '../../dto/article.dto';
import { Admin } from '../../common/decorators/admin.decorator';
import { ApiTags, ApiOperation, ApiResponse, ApiBody, ApiParam } from '@nestjs/swagger';
import { Category } from 'src/entities/category.entity';

@ApiTags('文章管理')
@Controller('api/admin/articles')
export class ArticleAdminController {
  categoryService: any;
  constructor(private readonly articleService: ArticleService) {}


  @Get()
  @Admin()
  @ApiOperation({
    summary: '获取所有文章（管理员）',
    description: '管理员获取所有文章，包括未审核的，支持分页、关键词搜索和状态过滤',
  })
  @ApiResponse({
    status: 200,
    description: '获取成功',
    type: [ArticleResponseDto],
  })
  @ApiResponse({
    status: 403,
    description: '无管理员权限',
  })
  findAllAdmin(
    @Query('page') page: number = 1,
    @Query('pageSize') pageSize: number = 10,
    @Query('keyword') keyword?: string,
    @Query('isActive') isActive?: number,
  ) {
    return this.articleService.findAllAdmin(page, pageSize, keyword, isActive);
  }

  @Put(':id')
  @Admin()
  @ApiOperation({
    summary: '更新文章内容（管理员）',
    description: '管理员可以更新任意文章的内容，包括标题、内容、封面图、标签等',
  })
  @ApiParam({
    name: 'id',
    description: '文章ID',
    example: '1',
  })
  @ApiBody({
    type: UpdateArticleDto,
    description: '更新的文章信息',
  })
  @ApiResponse({
    status: 200,
    description: '更新成功',
    type: ArticleResponseDto,
  })
  @ApiResponse({
    status: 403,
    description: '无管理员权限',
  })
  @ApiResponse({
    status: 404,
    description: '文章不存在',
  })
  update(@Param('id') id: string, @Body() updateArticleDto: UpdateArticleDto) {
    return this.articleService.updateAdmin(+id, updateArticleDto);
  }

  @Put(':id/status')
  @Admin()
  @ApiOperation({
    summary: '更新文章状态（管理员）',
    description: '管理员更新文章的审核状态',
  })
  @ApiParam({
    name: 'id',
    description: '文章ID',
    example: '1',
  })
  @ApiBody({
    schema: {
      example: {
        isActive: 1, // -1待审核，0审核失败，1审核成功
      },
    },
  })
  @ApiResponse({
    status: 200,
    description: '更新成功',
    type: ArticleResponseDto,
  })
  @ApiResponse({
    status: 403,
    description: '无管理员权限',
  })
  @ApiResponse({
    status: 404,
    description: '文章不存在',
  })
  updateArticleStatus(@Param('id') id: string, @Body('isActive') isActive: number) {
    return this.articleService.updateStatus(+id, isActive);
  }


  @Get(':id')
  @Admin()
  @ApiOperation({
    summary: '获取文章详情（管理员）',
    description: '管理员可以获取所有人的所有文章，包括未审核的',
  })
  @ApiParam({
    name: 'id',
    description: '文章ID',
    example: '1',
  })
  @ApiResponse({
    status: 200,
    description: '获取成功',
    type: ArticleResponseDto,
  })
  @ApiResponse({
    status: 403,
    description: '无管理员权限',
  })
  @ApiResponse({
    status: 404,
    description: '文章不存在',
  })
  findOneAdmin(@Param('id') id: string) {
    return this.articleService.findOne(+id, undefined, true);
  }


}